package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
@SuppressWarnings("all")
@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String name, String pwd) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select *from t_user where u_username = ? and u_pwd = ?");
            ps.setObject(1, name);
            ps.setObject(2, pwd);

            rs = ps.executeQuery();
            if (rs.next()) {
                user = this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return user;
    }

    private UserBean getUser() throws SQLException {
        UserBean user = new UserBean();
        user.setId(rs.getInt("pk_userId"));
        user.setUsername(rs.getString("u_username"));
        user.setPwd(rs.getString("u_pwd"));
        user.setNickname(rs.getString("u_nickname"));
        user.setEmail(rs.getString("u_email"));
        user.setImgPath(rs.getString("u_imgPath"));
        user.setDate(LocalDate.parse(rs.getString("u_date")));

        return user;
    }

    @Override
    public void add(UserBean user) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_user(u_username,u_pwd,u_nickname,u_imgPath,u_email,u_date)" +
                    "values (?,?,?,?,?,?)");
            ps.setObject(1,user.getUsername());
            ps.setObject(2,user.getPwd());
            ps.setObject(3,user.getNickname());
            ps.setObject(4,user.getImgPath());
            ps.setObject(5,user.getEmail());
            ps.setObject(6,user.getDate());

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
    }

    @Override
    public Boolean findByUsername(String username) {
        Boolean flag = true;
        this.setConnection();
        try {
            ps = con.prepareStatement("select *from t_user where u_username = ?");
            ps.setObject(1,username);

            rs = ps.executeQuery();
            if (rs.next()) {
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
        return flag;
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where pk_userId = ?");
            ps.setObject(1,id);

            rs = ps.executeQuery();
            if (rs.next()) {
                user = this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }

        return user;
    }
}
